;(function(){
    var zUI = new Object();
    zUI.util = {
        digit: function(a,b){
            var i="";a=String(a);
            b=b||2;for(var n=a.length;n<b;n++)i+="0";
            return a<Math.pow(10,b)?i+(0|a):a
        },
        fmtDate: function(date,fmt){
            var n=new Date(date||new Date);
            var a=[this.digit(n.getFullYear(),4),this.digit(n.getMonth()+1),this.digit(n.getDate())],
                o=[this.digit(n.getHours()),this.digit(n.getMinutes()),this.digit(n.getSeconds())];
            return fmt=fmt||"yyyy-MM-dd HH:mm:ss",
                fmt.replace(/yyyy/g,a[0]).replace(/MM/g,a[1]).replace(/dd/g,a[2]).replace(/HH/g,o[0]).replace(/mm/g,o[1]).replace(/ss/g,o[2])
        },
        isEmpty: function(a){
            return a=== null || a==='' || a === undefined;
        },
        safeToString: function(a,b){
            return this.isEmpty(a) ? b : a;
        },
        isF: function(a){
            return typeof a === 'function';
        },
        trim: function(v){
            return this.isEmpty(v) ? '' : v.replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' );
        },
        htmlEncode: function(s){
            return (typeof s != "string") ? s :
                s.replace(/"|&|'|<|>|[\x00-\x20]|[\x7F-\xFF]|[\u0100-\u2700]/g,
                    function($0){
                        var c = $0.charCodeAt(0), r = ["&#"];
                        c = (c == 0x20) ? 0xA0 : c;
                        r.push(c); r.push(";");
                        return r.join("");
                    });
        },
        arrayJoin: function(a,d){
            if(a instanceof Array) {
                var c="";
                for(var b=0;b<a.length;b++){
                    if(b>0){
                        c+=d;
                    }
                    c+=a[b];
                }
                return c;
            }
            return"";
        },
        arrayContain: function(a,c){
            if(a instanceof Array){
                for(var b=0;b<a.length;b++){
                    if(c===a[b]){
                        return true;
                    }
                }
            }
            return false;
        },
        UUID: function () {
            var str = '0123456789abcdefghijklmnopqrstuvwxyz';
            var arr = [];
            for(var i = 0; i < 32; i++){
                arr.push(str.substr(Math.floor(Math.random() * 37), 1))
            }
            return this.arrayJoin(arr,'');
        },
        each: function (obj,func) {
            if(obj){
                for (var i = 0; i < obj.length; i++) {
                    func(i,obj[i]);
                }
            }
        },
        setTimeout: function(callback, delay) {
            var setTimeoutId, start, remaining = delay;
            var tmo = {
                pause: function () {
                    window.clearTimeout(setTimeoutId);
                    remaining -= new Date() - start;
                    return remaining;
                },
                play: function () {
                    start = new Date();
                    window.clearTimeout(setTimeoutId);
                    setTimeoutId= window.setTimeout(callback, remaining);
                }
            };
            tmo.play();
            return tmo;
        },
        call: function(f){
            var arg = [];
            for (var i = 1; i < arguments.length; i++) {
                arg.push(arguments[i]);
            }

            if(this.isF(f)){
                return f.apply(f,arg);
            }
            return null;
        },
        copyToClipboard: function(text) {
            var textArea = document.createElement("textarea");
            textArea.style.position = 'fixed';
            textArea.style.top = '0';
            textArea.style.left = '0';
            textArea.style.width = '2em';
            textArea.style.height = '2em';
            textArea.style.padding = '0';
            textArea.style.border = 'none';
            textArea.style.outline = 'none';
            textArea.style.boxShadow = 'none';
            textArea.style.background = 'transparent';
            textArea.value = text;
            document.body.appendChild(textArea);
            textArea.select();

            try {
                var successful = document.execCommand('copy');
                var msg = successful ? '成功复制到剪贴板' : '该浏览器不支持点击复制到剪贴板';
                return true;
            } catch (err) {
                //alert('该浏览器不支持点击复制到剪贴板');
                return false;
            }finally {
                document.body.removeChild(textArea);
            }


        }
    };
    zUI.table={
        config:{
            align: 'left',
            rowsName: 'rows',
            page: false,
            pageSizeName: 'pageSize',
            pageNumName: 'pageNum',
            totalName: 'total',
            pageSortName: '_sort',
            errorMsg: '接口请求失败',
            emptyMsg: '暂无数据',
            parseErrorMsg: '接口返回格式错误',
            pageSelects:[10,30,50,100,200,300],
            pageSize: 10,
            emptyOption: 'after',
            aj:{
                type:'POST',
            },
            rowDetailExpand: false
        },
        load: function (set) {
            var res = {};
            var elem = zUI.dom.get(set.elem);
            if(!elem){
                return;
            }

            var u = zUI.util;
            var id = $(elem).attr('id');
            var cols = set.cols||[];
            var url = set.url||'';
            var page = !u.isEmpty(url)||set.page||zUI.table.config.page;

            var data = set.data||[];
            res.rows = data instanceof Array ? data : [];

            var queryParam = set.queryParam||{};

            var pageSizeName = set.pageSizeName||zUI.table.config.pageSizeName;
            var pageNumName = set.pageNumName||zUI.table.config.pageNumName;
            var pageSortName = set.pageSortName||zUI.table.config.pageSortName;
            var rowsName = set.rowsName||zUI.table.config.rowsName;
            var totalName = set.totalName||zUI.table.config.totalName;
            var emptyOption = set.emptyOption||zUI.table.config.emptyOption;
            var nowrap = set.nowrap !== false;
            var beforeQuery = set.beforeQuery||'';
            var onLoadData = set.onLoadData||'';
            var onLoadTable = set.onLoadTable||'';
            var onToggleRow = set.onToggleRow||'';
            var intervalColor = set.intervalColor||[];
            var getHeight = set.getHeight;
            res.intervalColorIndex = 0;

            res.sorts = [];
            res.hasTool = false;
            if(set.tools instanceof Array){
                res.hasTool = true;
                res.tools = set.tools;
            }

            var hasRowDetail = u.isF(set.rowDetail);
            var tdCount = cols.length + (hasRowDetail?1:0);

            res.pageSize = set.pageSize|| zUI.table.config.pageSize;
            res.pageNum = 1;
            var getRowColor = function (ind) {
                if(intervalColor instanceof Array && intervalColor.length > 0){
                    return  intervalColor[(ind+1) % intervalColor.length];
                }
                return null;
            };


            if(cols.length > 0){
                var getStyle = function (col,d,ind,isHead) {
                    var str = '';
                    var sortable = url&&isHead&& col.sortable != null && col.sortable!==false;

                    var title = col.title;
                    if(isHead!== true){
                        title = u.safeToString(d[col.name],'');
                        if(typeof title === 'string'){
                            title = title.replace(/<.*?>/g,"");
                        }
                    }
                    var wid = parseInt(col.width);
                    var align = isHead===true ? 'center' : (col.align|| zUI.table.config.align);
                    if(!isNaN(wid)){
                        str += ' width="'+wid+'px"';
                    }
                    if(col.tip !== false){
                        str += ' title="'+title+'"';
                    }
                    str += ' class="align-'+align+(sortable ? ' sortable no-select':'')+'"';
                    if(!isHead ){
                        var colo = getRowColor(ind);
                        if(colo != null){
                            str += ' style="background-color:'+colo+'"';
                        }
                    }

                    return str;
                };

                var pageSelects = set.pageSelects|| zUI.table.config.pageSelects;
                if(!u.arrayContain(pageSelects,this.pageSize)){
                    this.pageSize = pageSelects[0];
                }
                var gtr = '';
                res.groups = [];
                res.groupCnt = {};
                res.hasGroup = false;

                for (var i = 0; i < cols.length; i++) {
                    var col = cols[i];
                    var group = col.group;
                    col.name = col.name||('col'+i);

                    if(col.type!=='checkbox'&& !u.isEmpty(group)){
                        res.hasGroup = true;
                        var cnt = (res.groupCnt[group]||0)+1;
                        res.groupCnt[group] = cnt;
                        if(cnt === 1){
                            res.groups.push(group);
                        }
                    }
                }
                var str = '<div id="'+id+'_table_wrap" class="zUI-table-wrap'+(page === true ? '' : ' no-page')+(res.hasGroup ? ' second-head':'')+(res.hasTool ? ' three-head':'')+'" style="';
                if(set.height){
                    str += 'height:'+set.height+';';
                }
                if(set.width){
                    str += 'width:'+set.width+';';
                }
                str+='">';
                if(res.hasTool){
                    str += '<div id="'+id+'_table_tool_wrap" class="zUI-table-tool-wrap">';
                    for(var k in res.tools){
                        var tm = res.tools[k];
                        if(tm.name){
                            str += '<span class="zUI-table-tool">';
                            if(tm.ico){
                                str += '<i class="zUI-icon zUI-blue2 '+tm.ico+'" id="'+id+'_tool_'+k+'"></i>';
                            }
                            str += tm.name+'</span>';
                        }

                    }
                    str += '</div>';
                }
                str += '<div class="zUI-table-split"></div>' +
                    '<div class="zUI-table-header-wrap" id="'+id+'_head_wrap">\n' +
                    '   <table id="'+id+'_head" class="zUI-table nowrap" style="width: 100%">\n';

                var rowStr = '';
                rowStr += '<tr>';
                if(hasRowDetail){
                    if(res.hasGroup){
                        gtr += '<th name="zUI-rowdetail-toggle" rowspan="2" width="35px"></th>';
                    }else{
                        rowStr += '<th width="35px" name="zUI-rowdetail-toggle"></th>';
                    }
                }
                var getSort = function (col) {
                    var sortable = url&&col.sortable != null && col.sortable!==false;
                    var sort = typeof col.sortable === 'boolean' ? null : (col.sortable==='desc'? 'desc':'asc');
                    var sortableStr = '';
                    if(sortable){
                        sortableStr += '<i class="zUI-icon';
                        if(sort != null){
                            var si = sort=== 'desc'?'-':'+';
                            res.sorts.push(si+col.name);
                            sortableStr += ' zUI-blue2 fa fa-sort-'+sort;
                        }else{
                            sortableStr += ' fa fa-sort zUI-gray';
                        }
                        sortableStr+='"></i>';
                    }
                    return sortableStr;
                };
                for (var i = 0; i < cols.length; i++) {
                    var col = cols[i];

                    var wid = parseInt(col.width);
                    var wstr = '';
                    if(!isNaN(wid)){
                        wstr = ' width="'+wid+'px"';
                    }
                    if(col.type==='checkbox'){

                        if(res.hasGroup){
                            gtr += '<th name="zUI_ck_box" rowspan="2"'+wstr+'>'+'<input type="checkbox" id="'+id+'_checkbox_all">'+'</th>'
                        }else{
                            rowStr += '<th name="zUI_ck_box"';
                            rowStr += wstr;
                            rowStr += '>';
                            rowStr += '<input type="checkbox" id="'+id+'_checkbox_all">';
                        }
                    }else{
                        var cnt = res.groupCnt[col.group]||0;

                        if(res.hasGroup){
                            for (var j = 0; j < res.groups.length; j++) {
                                if(res.groups[j] === col.group){
                                    res.groups.splice(j,1);
                                    gtr+= '<th colspan="'+ cnt +'" title="'+u.safeToString(col.group,'')+'">'+u.safeToString(col.group,'')+'</th>';
                                    break;
                                }
                            }
                            if(!res.groupCnt[col.group]){
                                gtr+= '<th name="'+col.name+'" rowspan="2" '+getStyle(col,{},i,true)+'>'+col.title+getSort(col)+'</th>';
                            }else{
                                rowStr += '<th name="'+col.name+'"';
                                rowStr += getStyle(col,{},i,true);
                                rowStr += '>'+col.title+getSort(col);
                            }
                        }else{
                            rowStr += '<th name="'+col.name+'"';
                            rowStr += getStyle(col,{},i,true);
                            rowStr += '>'+col.title+getSort(col);
                        }

                    }
                    rowStr+='</th>'
                }
                if(gtr){
                    str += '<tr>'+gtr+'</tr>';
                }
                str += rowStr;
                str+=    '</tr>\n' +
                    '   </table>\n' +
                    '</div>\n' +
                    '<div class="zUI-table-content-wrap" id="'+id+'_body_wrap">\n' +
                    '   <table id="'+id+'_body" class="zUI-table'+(nowrap ? ' nowrap' : '')+'" style="width: 100%">\n' +
                    '   </table>\n' +
                    '</div>\n';
                if(page === true){
                    str+='<div class="zUI-table-page-wrap" id="'+id+'_page_wrap"><i class="zUI-icon fa fa-angle-double-left first disabled" title="首页"></i>' +
                        '<i class="zUI-icon fa fa-angle-left pre disabled" title="上一页"></i><font class="inline">共0页</font><input class="zUI-input inline" value="1"><button class="zUI-btn ssm inline">跳转</button>' +
                        '<i class="zUI-icon fa fa-angle-right next disabled" title="下一页"></i>' +
                        '<i class="zUI-icon fa fa-angle-double-right last disabled" title="尾页"></i><span class="inline"><font class="inline">共0条</font><select class="zUI-input inline">';
                    for (var q = 0; q < pageSelects.length; q++) {
                        var ps = pageSelects[q];
                        str += '<option '+(res.pageSize==ps ? 'selected' : '')+' value="'+ps+'">'+ps+'</option>'
                    }
                    str +='</select>条/页</span></div>';
                }

                str += '</div>';

            }
            $(elem).after($(str)).remove();
            var checkAll = $("#"+id+'_checkbox_all');
            var tw = $("#"+id+"_table_wrap");
            var cbw = $("#"+id+"_body_wrap");
            var chw = $("#"+id+"_head_wrap");
            var ct = $("#"+id+"_table_tool_wrap");
            var cb = $("#"+id+"_body");
            var ch = $("#"+id+"_head");
            cbw.scroll(function (e) {
                chw.scrollLeft(cbw.scrollLeft());
                /*if(cbw.scrollTop() == 0){
                    cbw.removeClass('tbt');
                }else{
                    cbw.addClass('tbt');
                }*/
            });
            res.width = function () {
                var w = null;
                if(arguments.length > 0){
                    w = arguments[0];
                }
                if(w != null){
                    tw.width(w);
                    res.resize1();
                }else{
                    return tw.width();
                }
            }
            res.height = function () {
                var w = null;
                if(arguments.length > 0){
                    w = arguments[0];
                }
                if(w != null){
                    tw.height(w);
                    res.resize1();
                }else{
                    return tw.height();
                }
            }
            var sortTrs = ch.children().find('th.sortable');
            res.resetSort = function(q){
                sortTrs.each(function (si,so) {
                    var name = $(so).attr('name');
                    var sortO = $(so).find('i');
                    sortO.removeClass('fa');
                    sortO.removeClass('fa-sort-asc');
                    sortO.removeClass('fa-sort-desc').removeClass('zUI-blue2');
                    sortO.removeClass('fa-sort');
                    sortO.addClass('fa fa-sort zUI-gray');

                });
                res.sorts = [];
                if(q!== false){
                    res.query();
                }
            }
            res.collectSort = function(){
                res.sorts = [];
                sortTrs.each(function (si,so) {
                    var name = $(so).attr('name');
                    var sortO = $(so).find('i');
                    if(sortO.hasClass('fa-sort-asc')){
                        res.sorts.push("+"+name);
                    }else if(sortO.hasClass('fa-sort-desc')){
                        res.sorts.push("-"+name);
                    }
                });
                return res.sorts;
            }
            sortTrs.click(function () {
                var name = $(this).attr('name');
                var sortO = $(this).find('i');
                var asc = sortO.hasClass('fa-sort-asc');
                var desc = sortO.hasClass('fa-sort-desc');
                var soc = sortO.hasClass('fa-sort');
                var onSort = set.onSort;
                var doS = u.call(onSort,name,asc?'desc':(desc?'cancle':'asc'))!== false;
                if(doS){
                    sortO.removeClass('fa');
                    sortO.removeClass('fa-sort-asc');
                    sortO.removeClass('fa-sort-desc');
                    sortO.removeClass('fa-sort');
                    sortO.removeClass('zUI-gray').removeClass('zUI-blue2');
                    if(asc){
                        sortO.addClass('fa fa-sort-desc zUI-blue2');
                    }else if(desc){
                        sortO.addClass('fa fa-sort zUI-gray');
                    }else if(soc){
                        sortO.addClass('fa fa-sort-asc zUI-blue2');
                    }
                    res.collectSort();
                    if(url){
                        res.query();
                    }
                }
            }).onselectstart = function () {
                return false;
            };
            if(res.hasTool){
                ct.children('.zUI-table-tool').each(function (i,o) {
                    var tm = i < res.tools.length ? res.tools[i] : null;
                    if(tm != null && u.isF(tm.func)){
                        $(o).click(tm.func);
                    }
                });
            }
            if(checkAll.length > 0){
                checkAll.click(function () {
                    $('input[name='+id+'_checkbox]').prop('checked',checkAll.prop('checked'));
                });
                res.getCheckedData = function () {
                    var $1 = [];
                    $('input[name='+id+'_checkbox]:checked').each(function (ei,eo) {
                        var inde = parseInt($(eo).attr('data-index'));
                        $1.push(res.rows[inde]);
                    });
                    return $1;
                }
            }

            if(page === true){
                var cp = $("#"+id+"_page_wrap");
                cp.children().find('select').eq(0).bind('change',function () {
                    res.pageNum = 1;
                    res.pageSize = $(this).val();
                    res.query();
                });
                cp.children('button').bind('click',function () {
                    var v = cp.children('input').val();
                    var to;
                    if(isNaN(to=parseInt(v))){
                        to = 1;
                    }
                    res.pageNum = Math.min(to,res.totalPage);
                    res.query();
                });

                res.setPage = function (rows,total) {
                    cp.children('i').addClass('disabled');
                    res.totalPage = parseInt(total/res.pageSize) + ((total % res.pageSize)===0 ? 0 : 1);
                    res.pageNum = Math.min(res.totalPage,res.pageNum);
                    cp.children('input').val(res.pageNum);
                    cp.children('font').eq(0).text('共'+res.totalPage+'页');
                    cp.children().find('font').eq(0).text('共'+total+'条');
                    var is = cp.children('i').unbind('click');
                    if(res.pageNum > 1){
                        is.eq(1).removeClass('disabled').click(function () {
                            res.pageNum -- ;
                            res.query();
                        });
                        is.eq(0).removeClass('disabled').click(function () {
                            res.pageNum = 1;
                            res.query();
                        });
                    }
                    if(res.pageNum < res.totalPage){
                        is.eq(2).removeClass('disabled').click(function () {
                            res.pageNum ++ ;
                            res.query();
                        });
                        is.eq(3).removeClass('disabled').click(function () {
                            res.pageNum = res.totalPage;
                            res.query();
                        });
                    }
                };
            }

            res.readerData = function(d){
                cb.empty();
                if(d instanceof Array && d.length > 0){
                    for (var i = 0; i < d.length; i++) {
                        var rowDetailExpand = zUI.table.config.rowDetailExpand;
                        if(u.isF(set.rowDetailExpand)){
                            rowDetailExpand = set.rowDetailExpand(d[i],i);
                        }
                        var html = '<tr data-index="'+i+'">';
                        var rd = false;
                        var colo = getRowColor(i);
                        if(hasRowDetail){
                            rd = set.rowDetail(d[i],i);
                            html += '<td name="zUI-rowdetail-toggle" class="align-center" style= "width:35px;';

                            if(colo != null){
                                html += 'background-color:'+colo+';';
                            }
                            html += '">';
                            if(rd !== false){
                                html += '<i class="zUI-icon rowdetail-toggle fa fa-angle-double-'+(rowDetailExpand?'down':'right')+'"></i>';
                            }
                            html += '</td>';
                        }
                        for (var j = 0; j < cols.length; j++) {
                            var name = cols[j].name;
                            if(cols[j].type==='checkbox'){
                                html += '<td class="align-center" name="zUI_ck_box"';
                                if(colo != null){
                                    html += ' style="background-color: '+colo+'"';
                                }
                                html += '><input type="checkbox" name="'+id+'_checkbox" data-index="'+i+'">';
                                html += '</td>';
                            }else{
                                html += '<td name="'+cols[j].name+'"';
                                html += getStyle(cols[j],d[i],i);
                                html += '>';
                                if(u.isF(cols[j].format)){
                                    html += cols[j].format(d[i][name],d[i],i);
                                }else{
                                    html += d[i][name];
                                }
                                html += '</td>';
                            }

                        }
                        html += '</tr>';
                        var dtr = $(html);
                        cb.append(dtr);
                        dtr.find('.rowdetail-toggle').click(function () {
                            var ob = $(this).parent().parent();
                            var ii = parseInt(ob.attr('data-index'));
                            var dtr = cb.find('tr[name=rowdetail-toggle-'+ii+']');

                            if(dtr.length > 0){
                                $(this).removeClass('fa-angle-double-down').removeClass('fa-angle-double-right');
                                if(dtr.eq(0).is(':visible')){
                                    dtr.fadeOut(100,res.resize1);
                                    $(this).addClass('fa-angle-double-right');
                                    u.call(onToggleRow,d[ii],ii,-1);
                                }else{
                                    dtr.fadeIn(100,res.resize1);
                                    $(this).addClass('fa-angle-double-down');
                                    u.call(onToggleRow,d[ii],ii,1);
                                }
                            }
                        });
                        if(hasRowDetail){
                            if(rd !== false){
                                if(!u.isF(rd)){
                                    cb.append('<tr name="rowdetail-toggle-'+i+'" '+(rowDetailExpand?'':' style="display:none"')+'><td class="rowdetail-toggle-td" colspan="'+tdCount+'">'+rd+'</td></tr>');
                                }else{
                                    var vl = u.call(rd);
                                    if(vl !== false){
                                        cb.append('<tr name="rowdetail-toggle-'+i+'" '+(rowDetailExpand?'':' style="display:none"')+'>'+vl+'</tr>');
                                    }
                                }
                            }
                        }
                    }
                    if(checkAll.length > 0){
                        $('input[name='+id+'_checkbox]').click(function () {
                            var arr = $('input[name='+id+'_checkbox]');
                            var ej = arr.length;
                            var chd = ej > 0;
                            for (var ei = 0; ei < ej; ei++) {
                                if($(arr[ei]).prop('checked')===false){
                                    chd = false;
                                    break;
                                }
                            }
                            checkAll.prop('checked',chd);
                        });

                    }
                }
                res.resize1();
                if(d.length === 0){
                    chw.prev().width(0);
                }
            };
            if(hasRowDetail){
                res.expandAllRowDetail = function () {
                    $(cb).find('i.rowdetail-toggle.fa-angle-double-right').click();
                };
                res.collapseAllRowDetail = function () {
                    $(cb).find('i.rowdetail-toggle.fa-angle-double-down').click();
                }
            }
            res.toggleRowDetail = function(index,status){
                if(status != null){
                    var clazz = status ==='show' ? '.fa-angle-double-right':'.fa-angle-double-down';
                    cb.find('.rowdetail-toggle'+clazz).eq(index).click();
                }else{
                    cb.find('.rowdetail-toggle').eq(index).click();
                }

            };
            res.resize1 = function(){
                var gh = parseInt(u.call(getHeight));
                if(!isNaN(gh)){
                    tw.height(gh);
                }
                ch.width(cb.outerWidth());
                //chw.prev().width(cbw.width()-cb.width()).height(cbw.height());
                var scw = cbw[0].offsetWidth-cbw[0].clientWidth;
                chw.prev().width(scw).height(chw.height()+cbw.height());
                chw.width(cbw.outerWidth()-scw);
                cb.children('tr').each(function (i,o) {
                    $(o).children('td').each(function (ii,oo) {
                        var nm = $(oo).attr('name');
                        if(nm){
                            $(oo).width(ch.find('th[name='+nm+']').width());
                        }

                    });
                });

            };
            res.collectParam = function(){
                var para = zUI.form.getData(set.queryForm);
                if(u.isF(queryParam)){
                    var pp = queryParam();
                    $.extend(para,pp);

                }
                para[pageSizeName] = res.pageSize;
                para[pageNumName] = res.pageNum;
                if(res.sorts.length > 0){
                    para[pageSortName] = u.arrayJoin(res.sorts,'|');
                }
                return para;
            };
            res.showMsgRow = function(msg){
                chw.prev().width(0);
                cb.empty();
                cb.append('<tr name="row-error"><td class="align-center" colspan="'+tdCount+'">'+msg+'</td></tr>');
            };
            res.query = function () {
                if(emptyOption !== 'after'){
                    cb.empty();
                }
                if(url){
                    var para = res.collectParam();
                    var aj = {};
                    $.extend(aj,zUI.table.config.aj);
                    aj.url = url;
                    aj.data = para;
                    aj.type = set.requestType||zUI.table.config.aj.type;
                    aj.error = function (e) {
                        res.showMsgRow(set.errorMsg||zUI.table.config.errorMsg);
                        u.call(set.onError,e);
                    };
                    aj.complete = function (r) {
                        if(set.showMask === false){
                            //
                        }else if(u.isF(set.closeMask)){
                            set.closeMask();
                        }else{
                            zUI.dialog.closeMask();
                        }
                        u.call(set.complete,r);
                    };
                    aj.success = function (result) {
                        if(emptyOption === 'after'){
                            cb.empty();
                        }
                        var getRow = result[rowsName],getTotal = result[totalName];
                        if(getRow == null || getTotal==null){
                            res.showMsgRow(set.parseErrorMsg||zUI.table.config.parseErrorMsg);
                            u.call(set.onError);
                            return;
                        }
                        u.call(onLoadData,getRow,getTotal);

                        if(getRow.length === 0){
                            res.showMsgRow(set.emptyMsg||zUI.table.config.emptyMsg);
                            return;
                        }

                        res.readerData(res.rows = getRow);
                        if(checkAll.length > 0){
                            checkAll.prop('checked',false);
                        }
                        if(page === true){
                            res.setPage(getRow,getTotal);
                        }
                        u.call(onLoadTable,getRow,getTotal);

                    };
                    u.call(beforeQuery,para);

                    if(set.showMask===false){
                        //
                    }else if(u.isF(set.showMask)){
                        set.showMask();
                    }else{
                        zUI.dialog.showMask();
                    }
                    $.ajax(aj);
                }else{
                    var from = 0,to = res.rows.length;
                    if(page){
                        from = res.pageSize * (res.pageNum-1);
                        to = parseInt(res.pageSize) + from;
                        if(from > res.rows.length){
                            from = res.rows.length;
                        }
                        if(to > res.rows.length){
                            to = res.rows.length;
                        }
                    }

                    var list = res.rows.slice(from,to);
                    if(list.length == 0){
                        res.showMsgRow(set.emptyMsg||zUI.table.config.emptyMsg);
                    }else{
                        res.readerData(list);
                        if(page){
                            res.setPage(list,res.rows.length);
                        }
                    }

                }

            };
            res.getData = function(index){
                return res.rows.length > index ? res.rows[index] : null;
            };

            if(set.queryBtn){
                $(set.queryBtn).bind('click',res.query);
            }
            if(set.autoInit !== false){
                res.query();
            }


            $(window).resize(res.resize1);

            return res;
        }
    };
    zUI.code = {
        keyWords:{
            java: ['class','package','import','public','private','static','protected','abstract','implements','entends',
                'final','void','new','return','default','while','for','if','else','switch','case','when','throw','throws',
                'try','catch','finally','synchronized','const','int','float','double','short','char','byte','true','false'],
            js: ['public','private','static','protect','final','return','delete','default','function','var','let','const','Number','true','false'],
        },
        render: function (set) {
            var elem = set.elem||'';
            var value = set.value||'';
            var style = set.style||'java'.toLowerCase();
            var $obj = document.querySelectorAll(elem);
            var kws = this.keyWords[style];
            var loadOne = function(tab,index){

                var textArr = (zUI.util.isEmpty(value) ? $(tab).html() : value).split("\n");

                if(textArr.length > 0){
                    var zs = -1;
                    var btn = dom.createElem({name:'button',clazz:'zUI-btn zUI-btn-null ssm info',text:'复制'});
                    btn.onclick = function(){
                        var string = "";
                        $(tab).find('.zUI-code-line').each(function (i,o) {
                            if(i > 0){
                                string += '\n';
                            }
                            string += $(o).text();
                        });
                        var pr = zUI.util.copyToClipboard(string);
                        if(pr){
                            zUI.dialog.message({content:'复制成功',timer:false,delay:3000,close:true,level:'info'});
                        }
                    }
                    $(tab).html('').append(btn);
                    var isV = function (key,row,index) {
                        var pre = index > 0 ? row.substring(index-1,index) : null;
                        var nex = index + key.length  < row.length ? row.substring(index + key.length,index + key.length+1) : null;
                        var i = 0;
                        var arr = {'{':'','}':'','(':'',')':'',';':'',',':'',':':''};
                        if(pre == null || pre.trim().length == 0|| pre in arr){
                            i++;
                        }
                        if(nex == null || nex.trim().length == 0|| nex in arr){
                            i++;
                        }
                        if(i==2){
                            return true;
                        }

                    }
                    var setString = function (v) {
                        var sar1 = [];
                        var sar2 = [];
                        var ind = -1;
                        while ((ind = v.indexOf('\'',ind+1)) != -1){
                            if(ind == 0 || v.charAt(ind-1) !='\\'){
                                sar1.push(ind);
                            }
                        }
                        if(sar1.length > 1){
                            var s = [];
                            for (var i = 0,j = sar1.length; i < j; i++) {
                                if(i == 0){
                                    s.push(v.substring(0,sar1[0]));
                                }else{
                                    if(i%2==1){
                                        s.push('<span class="string">'+v.substring(sar1[i-1],sar1[i]+1)+'</span>');
                                    }else{
                                        s.push(v.substring(sar1[i-1]+1,sar1[i]));
                                    }

                                }
                            }
                            s.push(v.substring(sar1[sar1.length-1]+1));

                            return zUI.util.arrayJoin(s,'');
                        }
                        return v;

                    }
                    var buildV = function (v) {
                        v = setString(v);
                        for (var x in kws) {
                            if(v.indexOf(kws[x]) > -1){
                                v = setValue(kws[x],v);
                            }
                        }
                        return v;
                    }
                    var setValue = function (key,row) {
                        var index = 0;
                        while((index = row.indexOf(key,index))!= -1){
                            if(isV(key,row,index)){
                                var ss = '<span class="keyword">'+key+'</span>';
                                row = row.substring(0,index) + ss + row.substring(index + key.length);
                                index += ss.length;
                            }else{
                                index += key.length;
                            }
                        }
                        return row;
                    }
                    var tranOne = function (line) {
                        line = line.replace(/</g, '&lt;').replace(/>/g, '&gt;');
                        var ed1 = line.lastIndexOf("*/");
                        var ed2 = line.indexOf("/*");
                        var ind2 = line.indexOf("//");
                        if(zs == 1){

                            if(ed1 != -1){
                                var lv1 = line.substring(0,ed1+2);
                                var lv2 = line.substring(ed1+2);
                                lv2 = buildV(lv2);
                                line = '<span class="note">'+lv1+'</span>'+lv2;
                                zs = -1;
                            }else{
                                line = '<span class="note">'+line+'</span>';
                            }

                        }else{

                            if(ed1 > -1 && ed2 > -1){
                                var lv1 = line.substring(0,ed1);
                                var lv2 = line.substring(ed1,ed2+2);
                                var lv3 = line.substring(ed2+2);
                                lv1 = buildV(lv1);
                                lv3 = buildV(lv3);
                                line = lv1 + '<span class="note">'+lv2+'</span>' + lv3;
                            }else if(ed2 > -1){
                                zs = 1;
                                var lv1 = line.substring(0,ed2);
                                var lv2 = line.substring(ed2);
                                lv1 = buildV(lv1);
                                line = lv1 + '<span class="note">'+lv2+'</span>' ;
                            }else{
                                if(ind2 != -1){
                                    var lv1 = line.substring(0,ind2+2);
                                    var lv2 = line.substring(ind2+2);
                                    lv1 = buildV(lv1);
                                    line = lv1 + '<span class="note">'+lv2+'</span>';
                                }else{
                                    line = buildV(line);
                                }
                            }

                        }


                        return line;
                    }
                    for (var i = 0; i < textArr.length; i++) {
                        var seq = dom.createElem({name:'div',clazz:'zUI-code-seq',text:i+1});
                        var lineObj = dom.createElem({name:'div',clazz:'zUI-code-line'/*,html:line*/});
                        var comBine = tranOne(textArr[i]);
                        if(!comBine){
                            comBine = '<br>';
                        }
                        $(lineObj).html(comBine);

                        $(tab).append(seq).append(lineObj);
                    }
                }
            }
            for(var i=0;i<$obj.length;i++){
                loadOne($obj[i],i);
            }
        }
    };
    zUI.form = {
        reset: function(formObj,excludes){
            var form = typeof formObj === 'string' ? $(formObj) : formObj;
            var items = $(form).children().find('input,select,textarea');
            zUI.util.each(items,function (i,o) {
                var pass = false;
                if(excludes && o.id){
                    for(var i=0;i<excludes.length;i++){
                        if(id === excludes[i]){
                            pass = true;
                            break;
                        }
                    }
                }
                if(pass){
                    return;
                }
                var type = o.getAttribute('type')||'text';
                if(type === 'radio' || type === 'checkbox'){
                    o.checked = false;
                }else{
                    o.value = '';
                }
            });
            this.render();

        },
        getData: function(formObj){
            var res = {};
            var form = typeof formObj === 'string' ? $(formObj) : formObj;
            var items = $(form).find('input,select,textarea');
            for (var i = 0; i < items.length; i++) {
                var o = items[i];
                if(o.name){
                    var v = null;
                    if($(o).is('input')){
                        var type = o.getAttribute('type')||'text';

                        if(type === 'radio' || type === 'checkbox'){

                            v = o.checked ? o.value : v;
                        }else{
                            v = o.value||'';
                        }
                    }else{
                        v = o.value||'';
                    }
                    if(v !== null){
                        var oldV = res[o.name];
                        var newV;
                        if(oldV === undefined){
                            newV = v;
                        }else if(typeof oldV === 'string'){
                            newV = [oldV,v];
                        }else if(typeof oldV === 'object'){
                            oldV.push(v);
                            newV = oldV;
                        }
                        res[o.name] = newV;
                    }

                }
            }
            for(var n in res){
                if(typeof res[n]=== 'object'){
                    res[n] = res[n].toString();
                }
            }
            return res;
        },
        render: function (){
            var forms = $('.zUI-form');
            zUI.util.each(forms,function (i,o) {
                var radios = o.querySelectorAll('input.zUI-input[type=radio]');
                zUI.util.each(radios,function (j,r) {
                    dom.addClass(r,'zUI-hide');
                    var title = r.getAttribute('title')||'';
                    var disabled = (r.getAttribute('disabled')||'')=== 'disabled';
                    var name = r.getAttribute('name');
                    var gp = $('input[type=radio][name=\''+name+'\']');
                    var wrap = r.nextElementSibling;
                    var clazz = 'zUI-rc-wrap zUI-radio-wrap'+(r.checked === true ? ' zUI-checked':'')+(disabled ?' disabled':'');
                    if(wrap == null || !$(wrap).hasClass('zUI-rc-wrap')){
                        wrap = dom.createElem({name:'label'});
                        var text = document.createTextNode(title);
                        var radio = dom.createElem({name:'div',clazz:'zUI-radio'});
                        wrap.appendChild(radio);
                        wrap.appendChild(text);
                        $(r).after(wrap);
                    }
                    wrap.className = clazz;
                    if(r.checked !== true){
                        dom.removeClass(wrap,'zUI-checked');
                    }
                    var f = function(wp,ro){
                        return function () {
                            var err = 0;
                            zUI.util.each(gp,function(ii,oo){
                                if(oo.disabled && oo.checked){
                                    err++;
                                }
                            });
                            if(err> 0){
                                return;
                            }
                            var ck = $(wp).hasClass('zUI-checked');
                            if(!ck){
                                zUI.util.each(gp,function(ii,oo){
                                    if (oo === ro){
                                        oo.checked = true;
                                        dom.addClass(wp,'zUI-checked');
                                    }else{
                                        oo.checked = false;
                                        dom.removeClass(oo.nextElementSibling,'zUI-checked');
                                    }
                                })
                            }
                        }

                    }(wrap,r);
                    if(!disabled){
                        wrap.onclick = f;
                    }

                });
                var checkBoxs = o.querySelectorAll('input.zUI-input[type=checkbox]');
                zUI.util.each(checkBoxs,function (j,r) {
                    dom.addClass(r,'zUI-hide');
                    var title = r.getAttribute('title')||'';
                    var disabled = (r.getAttribute('disabled')||'')=== 'disabled';
                    var name = r.getAttribute('name');
                    var wrap = r.nextElementSibling;
                    var clazz = 'zUI-rc-wrap zUI-checkbox-wrap'+(r.checked === true ? ' zUI-checked':'')+(disabled ? ' disabled':'');
                    if(wrap == null || !$(wrap).hasClass('zUI-rc-wrap')){
                        wrap = dom.createElem({name:'label'});
                        var text = document.createTextNode(title);
                        var radio = dom.createElem({name:'div',clazz:'zUI-checkbox'});
                        wrap.appendChild(radio);
                        wrap.appendChild(text);
                        $(r).after(wrap);
                    }
                    wrap.className = clazz;
                    var f = function(wp,ro){
                        return function () {
                            var ck = $(wp).hasClass('zUI-checked');
                            if(ck){
                                dom.removeClass(wp,'zUI-checked');
                                ro.checked = false;
                            }else{
                                dom.addClass(wp,'zUI-checked');
                                ro.checked = true;
                            }
                        }

                    }(wrap,r);
                    if(!disabled){
                        wrap.onclick = f;
                    }

                });
            });
        }
    }
    zUI.onLoad = function(func){
        var oldonload = window.onload;
        if(typeof window.onload != 'function'){
            window.onload = func;
        }else{
            window.onload = function(){
                oldonload();
                func();
            }
        }

    };

    zUI.event={
        map: {},
        bind: function(key,obj,type,fn,opt){
            obj.addEventListener(type,fn,opt=!!opt);
            this.map[key] = {obj:obj,type:type,fn:fn,opt:opt};
        },
        unbind: function(key){
            var d = this.map[key];
            if(d){
                if(window.removeEventListener){
                    d.obj.removeEventListener(d.type, d.fn,d.opt);
                }else if(window.detachEvent){
                    d.obj.detachEvent('on' + d.type, d.fn,d.opt);
                }
            }
        }
    };

    var dom = {
        get: function(elem){
            return typeof elem === 'string' ? document.querySelector(elem) : elem;
        },
        isVisable: function(elem){
            return $(elem).is(":visible");
        },
        getScrollTop: function (){
            var scroll_top = 0;
            if (document.documentElement && document.documentElement.scrollTop) {//如果非IE
                scroll_top = document.documentElement.scrollTop;
            }
            else if (document.body) {//IE浏览器
                scroll_top = document.body.scrollTop;
            }
            return scroll_top;
        },
        removeEvent: function(elem,type,fn){
            if(window.removeEventListener){
                elem.removeEventListener(type, fn);
            }else if(window.detachEvent){
                elem.detachEvent('on' + type, fn);
            }
        },
        createElem: function(opt){
            var element = opt.name,clazz=opt.clazz,id=opt.id,text=opt.text,html=opt.html;
            var d = document.createElement(element);
            if(clazz){
                d.setAttribute('class',clazz);
            }
            if(id){
                d.setAttribute('id',id);
            }
            if(text){
                d.appendChild(document.createTextNode(text));
            }
            if(html){
                d.innerHTML = html;
            }
            return d;
        },
        addClass: function(o,c){
            $(o).addClass(c);
        },
        hasClass: function(o,c){
            return $(o).hasClass(c);
        },
        removeClass: function(o,c){
            $(o).removeClass(c);
        },
        swapNode: function (aNode,bNode){
            var aParent = aNode.parentNode;
            var bParent = bNode.parentNode;
            if(aNode && bNode){
                var aNode2 = aNode.cloneNode(true);//aNode 没有父节点
                bParent.replaceChild(aNode2,bNode);
                aParent.replaceChild(bNode,aNode);
                return aNode2;
            }
        },
        makeSVG: function(opt) {
            var tag=opt.name,id=opt.id,clazz=opt.clazz;
            var ns = 'http://www.w3.org/2000/svg';
            var el= document.createElementNS(ns, tag);
            el.setAttribute("class",clazz);
            el.setAttribute("id",id);
            return el;
        },
        isChild: function (child, parent) {
            var parentNode;
            if(child && parent) {
                parentNode = child.parentNode;
                while(parentNode) {
                    if(parent === parentNode) {
                        return true;
                    }
                    parentNode = parentNode.parentNode;
                }
            }
            return false;
        }
    };
    zUI.onLoad(zUI.form.render);
    zUI.dom=dom;

    window.zUI = zUI;
}());